<template>
  <el-card class="box-card" style="margin: 15px; width: 800px">
    <el-button
      type="primary"
      style="margin-bottom: 30px"
      icon="el-icon-plus"
      @click="addDate"
      >新增配置</el-button
    >
    <el-form
      :inline="true"
      :rules="rules"
      ref="myform"
      class="demo-form-inline"
      :model="form"
    >
      <div v-for="(item, index) in form.configList" :key="index">
        <p>挡位{{ index + 1 }}</p>
        <el-form-item
          label="充值金额"
          :prop="'configList.' + index + '.positionValue'"
          :rules="rules.positionValue"
        >
          <el-input
            placeholder="充值金额"
            v-model="item.positionValue"
          ></el-input>
        </el-form-item>
        <el-button type="primary" @click="show(item, index)"
          >配置红包</el-button
        >
        <el-button
          type="danger"
          @click="del(index)"
          icon="el-icon-delete"
          circle
        ></el-button>
      </div>
    </el-form>
    <el-button type="primary" @click="submit">提交</el-button>
    <redPack ref="redPack" @selectCoupon="getCoupon" />
  </el-card>
</template>
<script>
import redPack from './redpack.vue'
import { rechargePageList, rechargeSave } from '@/config/api'
import { times, divide } from "@/utils/tools"

export default {
  components: {
    redPack
  },
  data () {
    return {
      index: 0,
      form: {
        configList: []
      },
      rules: {
        positionValue: [
          { required: true, message: '请输入充值金额', trigger: 'blur' }],

      }
    }

  },
  created () {
    this.getData()

  },
  methods: {
    getCoupon (data) {
      console.log(data)
      this.form.configList[this.index].couponDTOList = data
    },
    show (data, index) {
      this.index = index
      this.$refs.redPack.show(data)
    },
    addDate () {
      this.form.configList.push({
        positionValue: undefined,
        couponDTOList: []
      })
    },
    getData () {
      rechargePageList().then(res => {
        this.form.configList = []
        res.data.result.list.map(item => {
          let positionValue = divide(item.positionValue, 100)
          let couponDTOList = []

          item.couponDTOList.map(item => {
            couponDTOList.push({
              id: item.id,
              preAmount: item.preAmount,
              couponKind: item.couponKind
            })
          })
          this.form.configList.push({ positionValue, couponDTOList })
        })
        if (res.data.result.list.length == 0) {
          this.form.configList.push({ positionValue: "", couponDTOLis: [] })
        }
        console.log(this.configList)
      })
    },
    del (index) {
      this.$confirm('此操作将确认该挡位, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.form.configList.splice(index, 1)

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    submit () {
      this.$refs['myform'].validate((valid) => {
        if (valid) {
          let params = JSON.parse(JSON.stringify(this.form.configList))
          params.map(item => {
            item.positionValue = times(item.positionValue, 100)
          })
          rechargeSave(params).then(res => {
            if (res.data.status == 200) {
              this.$message({
                type: 'success',
                message: '操作成功!'
              });
            }
            this.getData()

          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });


    }
  }
}
</script>